<template>
  <div>
    <v-tableTitle :btnConfig="btnConfig" :tableSelectData="tableSelectData" :title="$t('router.comSetting')" @del="del" @newCreate="newCreate"></v-tableTitle>
    <GTable ref="table" :hasCheck="true" :hasEdit="true" api="/v1/edi/plCompany/selectPlCompanyList" :columnSource="baseColumns" :cbFun="cbFun" @selection-change="data => tableSelectData = data"
      @setdialog="setdialog" />

    <el-dialog :title="dialogModule.title" :visible.sync="dialogModule.flag" center v-dialogDrag width="720px">
      <div v-tit="$t('router.comInfo')">
        <GForm ref="detail" :config="formConfig" :rules="formRules" v-model="form"></GForm>
      </div>
      <span slot="footer">
        <el-button type="primary" @click="handleConfirm">确定</el-button>
        <el-button @click="dialogModule.flag = false">取消</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
import tableTitle from 'commons/TableTitle'

export default {
  name: 'company',
  data() {
    return {
      tableSelectData: [],
      dialogModule: {
        loading: false,
        flag: false,
        title: '',
        data: null,
        type: ''
      },
      form: {
        companyNo: null,
        companyName: null,
        companyTel: null,
        companyMobile: null,
        companyAddr: null,
        companyType: 50002,
        companyStatus: 50102,
        remark: null
      },
      options:{
        type:[],
        status:[]
      }
    }
  },
  computed: {
    btnConfig() {
      return [
        {
          label: this.$t('common.create'),
          // certigier: !this.btnPermNameList.includes('EN010101'),
          invalid: 'newCreate'
        },
        {
          label: this.$t('common.delete'),
          // certigier: !this.btnPermNameList.includes('EN010102'),
          disabled: true,
          invalid: 'del'
        }
      ]
    },
    baseColumns() {
      return [
        {
          label: this.$t('cn.companyNo'),
          prop: 'companyNo',
          input: 'input'
        },
        {
          label: this.$t('cn.companyName'),
          prop: 'companyName',
          input: 'input'
        },
        // {
        //   label: this.$t('common.type'),
        //   prop: 'companyType',
        //   input: 'select',
        //   options:this.options.type
        // },
        {
          label: this.$t('app.status'),
          prop: 'companyStatus',
          input: 'select',
          options:this.options.status
        },
        {
          label: this.$t('config.phone'),
          prop: 'companyTel'
        },
        {
          label: this.$t('cn.phone'),
          prop: 'companyMobile'
        },
        {
          label: this.$t('config.address', { no: '' }),
          prop: 'companyAddr'
        },
        {
          label: this.$t('app.remark'),
          prop: 'remark'
        },
        {
          label: this.$t('app.addName'),
          prop: 'insUser'
        },
        {
          label: this.$t('app.addDate'),
          prop: 'insTime'
        },
        {
          label: this.$t('app.updateName'),
          prop: 'updUser'
        },
        {
          label: this.$t('app.updateDate'),
          prop: 'updTime'
        }
      ]
    },
    formConfig() {
      return [
        {
          prop: 'companyNo',
          type: 'input',
          label: this.$t('cn.companyNo')
        },
        {
          prop: 'companyName',
          type: 'input',
          label: this.$t('cn.companyName')
        },
        // {
        //   prop: 'companyType',
        //   type: 'select',
        //   label: this.$t('common.type'),
        //   options:this.options.type
        // },
        {
          prop: 'companyTel',
          type: 'input',
          label: this.$t('config.phone')
        },
        {
          prop: 'companyMobile',
          type: 'input',
          label: this.$t('cn.phone')
        },
        {
          prop: 'companyAddr',
          type: 'input',
          label: this.$t('config.address', { no: '' })
        },
        {
          prop: 'remark',
          type: 'textarea',
          label: this.$t('home1.remark')
        }
      ]
    },
    formRules() {
      return {
        companyNo: [
          {
            required: true,
            message: this.$inputPlease('cn.companyNo'),
            trigger: 'blur'
          }
        ],
        companyName: [
          {
            required: true,
            message: this.$inputPlease('cn.departName'),
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    handleConfirm() {
      this.$refs.detail.validate().then(valid => {
        const params = this._.cloneDeep(this.form)
        let promise
        if (this.dialogModule.type == 'edit') {
          promise = this.$post(
            `/v1/edi/plCompany/editPlCompanyById`,
            params
          )
        } else {
          promise = this.$post(`/v1/edi/plCompany/addPlCompany`, params)
        }
        promise.then(res => {
          this.$message.success(res.returnMgr)
          this.getTableData()
          this.dialogModule.flag = false
        })
      })
    },
    getInfoById(id) {
      this.$post('/v1/edi/plCompany/selectPlCompanyById', {
        id
      }).then(res => {
        this.form = res.data.item
        this.dialogModule.flag = true
      })
    },
    setdialog(data) {
      this.dialogModule.title = `编辑[${data.companyName}]公司信息`
      this.dialogModule.type = 'edit'
      this.dialogModule.data = data
      this.getInfoById(data.id)
    },
    newCreate() {
      this.dialogModule.title = '新增公司信息'
      this.dialogModule.type = 'add'
      this.dialogModule.data = null
      this.form = {
        companyNo: null,
        companyName: null,
        companyTel: null,
        companyMobile: null,
        companyAddr: null,
        companyType: 50002,
        companyStatus: 50102,
        remark: null
      }
      this.dialogModule.flag = true
    },
    //删除
    async del(data) {
      this.$post('/v1/edi/plCompany/deletePlApplicationByIds', data).then(
        res => {
          this.$message.success(res.returnMgr)
          this.getTableData()
        }
      )
    },
    getOptions(){
      this.$get('/codeLookup/CompanyType').then(res => {
        this.options.type = res.data.map(i => {
          i.label = i.description
          i.value = i.code
          return i
        })
      })
      this.$get('/codeLookup/CompanyStatus').then(res => {
        this.options.status = res.data.map(i => {
          i.label = i.description
          i.value = i.code
          return i
        })
      })
    },
    cbFun(res) {
      return { list: res.data.page.records, total: res.data.page.total }
    },
    async getTableData() {
      this.$refs.table.getTableData()
    }
    //设置弹框
  },
  mounted() {
    this.getOptions()
  },
  components: {
    'v-tableTitle': tableTitle
  }
}
</script>

<style scoped>
</style>
